/*******************************************************************************
** CopyRight (c) 2005-2008 SKY-MOBI Ltd. All rights reserved.
** Filename:  commdefine.h
** Creator:   huangxiaohua
** Version:   0.0
** Date:      
** Description:
 *******************************************************************************/
#ifndef __COMM_DEFFINE_H__
#define __COMM_DEFFINE_H__

#include "config.h"
#include "mrc_base.h"
#include "ext.h"
#include "extIdDef.h"
#include "sgl_types.h"

#include "debug.h"

#define LOCAL   static
#define API  


#if (defined(SDK_MOD) || defined(MODULE_INSERT) || defined(SDK_NO_PLUGIN))
#define PMRC_INIT(MODULE) 	mrc_init_ ## MODULE
#define PMRC_EVENT(MODULE)  	mrc_event_ ## MODULE
#define PMRC_PAUSE(MODULE)  	mrc_pause_ ## MODULE
#define PMRC_RESUME(MODULE)  mrc_resume_ ## MODULE
#define PMRC_EXITAPP(MODULE) mrc_exitApp_ ## MODULE
#define PMRC_EXT_RECV_APP_EVENT_EX(MODULE)	mrc_extRecvAppEventEx_ ## MODULE
#define PMRC_EXT_RECV_APP_EVENT(MODULE)		mrc_extRecvAppEvent_ ## MODULE
#else
#define PMRC_INIT(MODULE) 	mrc_init
#define PMRC_EVENT(MODULE)  	mrc_event
#define PMRC_PAUSE(MODULE)  	mrc_pause
#define PMRC_RESUME(MODULE)  mrc_resume
#define PMRC_EXITAPP(MODULE) mrc_exitApp
#define PMRC_EXT_RECV_APP_EVENT_EX(MODULE)	mrc_extRecvAppEventEx
#define PMRC_EXT_RECV_APP_EVENT(MODULE)		mrc_extRecvAppEvent
#endif

#define MRC_INIT(MODULE, VOID) PMRC_INIT(MODULE)(VOID)
#define MRC_EVENT(MODULE, CODE, P1, P2)  	PMRC_EVENT(MODULE)(CODE, P1, P2)
#define MRC_PAUSE(MODULE, VOID)  	PMRC_PAUSE(MODULE)(VOID)
#define MRC_RESUME(MODULE, VOID)  PMRC_RESUME(MODULE)(VOID)
#define MRC_EXITAPP(MODULE, VOID) PMRC_EXITAPP(MODULE)(VOID)
#define MRC_EXT_RECV_APP_EVENT_EX(MODULE, CODE, P0, P1, P2, P3, P4, P5)	PMRC_EXT_RECV_APP_EVENT_EX(MODULE)(CODE, P0, P1, P2, P3, P4, P5)
#define MRC_EXT_RECV_APP_EVENT(MODULE, APP, CODE, P1, P2)		PMRC_EXT_RECV_APP_EVENT(MODULE)(APP, CODE, P1, P2)


#define C_MRC_INIT(MODULE) PMRC_INIT(MODULE)()
#define C_MRC_EVENT(MODULE, CODE, P1, P2)  	PMRC_EVENT(MODULE)(CODE, P1, P2)
#define C_MRC_PAUSE(MODULE)  	PMRC_PAUSE(MODULE)()
#define C_MRC_RESUME(MODULE)  PMRC_RESUME(MODULE)()
#define C_MRC_EXITAPP(MODULE) PMRC_EXITAPP(MODULE)()
#define C_MRC_EXT_RECV_APP_EVENT_EX(MODULE, CODE, P0, P1, P2, P3, P4, P5)	PMRC_EXT_RECV_APP_EVENT_EX(MODULE)(CODE, P0, P1, P2, P3, P4, P5)
#define C_MRC_EXT_RECV_APP_EVENT(MODULE, APP, CODE, P1, P2)		PMRC_EXT_RECV_APP_EVENT(MODULE)(APP, CODE, P1, P2)


/**
 * \def BEGIN_REG_MAP
 * \brief register ext and extfunc in main moduler(uses  ON_REG_EXT_FUNCODE ,END_REG_MAP together)
 * \param MODULEREG reg.ext name for SDK
 * \param MODULEINIT the real ext name for SDK
 */
#ifdef SDK_MOD

#define BEGIN_REG_MAP(MODULE) \
int32 MRC_INIT(MODULE ## _init, void); \
int32 MRC_EVENT(MODULE ## _init, int32 code, int32 param0, int32 param1); \
int32 MRC_PAUSE(MODULE ## _init, void); \
int32 MRC_RESUME(MODULE ## _init, void); \
int32 MRC_EXITAPP(MODULE ## _init, void); \
\
int32 MRC_INIT(MODULE ## _reg,void) \
{ \
	int32 handle; \
	int32 i=0; \
 \
	mrcEnterFuncSt enterFunc; \
	enterFunc.mrc_init_f = PMRC_INIT(MODULE ## _init); \
	enterFunc.mrc_event_f = PMRC_EVENT(MODULE ## _init); \
	enterFunc.mrc_pause_f = PMRC_PAUSE(MODULE ## _init); \
	enterFunc.mrc_resume_f = PMRC_RESUME(MODULE ## _init); \
	enterFunc.mrc_exitApp_f = PMRC_EXITAPP(MODULE ## _init);
	
#else

#define BEGIN_REG_MAP(MODULE) \
int32 MRC_INIT(MODULE ## _reg,void) \
{ \
	int32 handle; \
	int32 i=0; 
	
#endif


/**
 * \def ON_REG_EXT_FUNCODE
 * \brief register ext and extfunc  in main moduler(uses  BEGIN_REG_MAP ,END_REG_MAP together)
 * \param EXTID the real ext ID
 * \param EXINAME the real ext filename 
 * \param FSTART reg func start ID
 * \param FEND reg func end ID
 */
#ifdef SDK_MOD 
#define ON_REG_EXT_FUNCODE(EXTID,EXINAME,EXVER,FSTART,FEND) \
	handle = mpsFpRegExt(EXTID, EXVER, mrc_getPackName(), EXINAME, &enterFunc); \
	if(handle){ \
		for(i = FSTART; i < FEND; i++) {mpsFpRegFunc(handle, i);} \
		return MR_SUCCESS;} 

#else
#define ON_REG_EXT_FUNCODE(EXTID,EXINAME,EXVER,FSTART,FEND) \
	SGL_TRACE("mrc_init of regist " EXINAME); \
	handle = mpsFpRegExt(EXTID, EXVER, mrc_getPackName(), EXINAME); \
	if(handle){ \
		for(i = FSTART; i < FEND; i++) {mpsFpRegFunc(handle, i);} \
		return MR_SUCCESS;} 

#endif
	

/**
 * \def END_REG_MAP
 * \brief register ext and extfunc  in main moduler(uses  ON_REG_EXT_FUNCODE ,END_REG_MAP together)
 * \param MODULEREG reg.ext name for SDK 
 */
#define END_REG_MAP(MODULE) \
return MR_FAILED;}\
int32 MRC_EVENT(MODULE ## _reg,int32 code, int32 param0, int32 param1)\
{return MR_SUCCESS;}\
int32 MRC_EXT_RECV_APP_EVENT_EX(MODULE ## _reg,int32 code, int32 p0, int32 p1, int32 p2, int32 p3, int32 p4, int32 p5)\
{ return MR_SUCCESS;}\
int32 MRC_EXT_RECV_APP_EVENT(MODULE ## _reg,int32 app, int32 code, int32 param0, int32 param1)\
{ return MR_SUCCESS;}\
int32 MRC_PAUSE(MODULE ## _reg,void)\
{ return 0;}\
int32 MRC_RESUME(MODULE ## _reg,void)\
{return 0;}\
int32 MRC_EXITAPP(MODULE ## _reg,void)\
{return 0;}



/**
 * \def ExtShell
 * \brief function shell of main moduler for packet API
 * \param EXTNAME moduler name
 * \param subCode API code in this moduler
 * \param p1-p5 parameters for API
 */
#define PExtShell(EXTNAME) EXTNAME##FuncShell
#define ExtShell(EXTNAME, subCode, p1, p2, p3, p4, p5) EXTNAME##FuncShell(subCode, p1, p2, p3, p4, p5)

/**
 * \def SET_EXT_FUNCSHELL
 * \brief register the real address of ext shell function (called in mrc_init() )  
 * \param EXTNAME moduler name
 * \param subCode API code in this moduler
 * \param p1-p5 parameters for API
 */
#define SET_EXT_FUNCSHELL(EXTNAME, code) MRC_EXITAPP(code, EXTNAME##FuncShell)

/**
  *
  */
#ifdef SDK_MOD
#define MAX_PACK_NAME_LEN   256
#else
#define MAX_PACK_NAME_LEN   32
#endif


#ifndef SDK_NO_PLUGIN
#define CALLFUNC(func, p0, p1, p2, p3, p4, p5)	\
	mrc_extSendAppEventEx(func, (int32)p0, (int32)p1, (int32)p2, (int32)p3, (int32)p4, (int32)p5)
#else
#define CALLFUNC(func, p0, p1, p2, p3, p4, p5)	\
	func((int32)p0, (int32)p1, (int32)p2, (int32)p3, (int32)p4, (int32)p5)
#endif
#endif



